<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// 
//  brand_model.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-05-31.
//  Copyright 2011 lishuzu. All rights reserved.
// 


class Brand_model extends CI_model{
	function __construct(){
		parent::__construct();
		$this->userinfo = $this->session->userdata('userinfo');
		$this->category = $this->config->item('category');
	}
	
	function get_brand_by_bid($bid){
		if ($bid) {
			$this->db->where('bid', $bid);
			//－1 为删除状态
			$this->db->where('status !=', '-1');
			$query = $this->db->get('brand');
			$brand = $query->row_array();
			$brand['catname'] = $brand['catid'] ? $this->category[$brand['catid']]['name'] : '未分类';
			$brand['logourl'] = base_url().'upload/logo/'.$brand['logo'];
			return $brand;
		}
		return false;
	}
	
	function get_brands(){
		$mybrands = array();
		//－1 为删除状态
		$this->db->where('status !=', '-1');
		$this->db->order_by('dateline', 'desc');
		$query = $this->db->get('brand');
		foreach ($query->result_array() as $row) {
			$row['logourl'] = base_url().'upload/logo/'.$row['logo'];
			$row['catname'] = $row['catid'] ? $this->category[$row['catid']]['name'] : '未分类';
			$data['brands'][$row['bid']] = $row;
		}
		return $data;
	}
	
	function get_brand_data(){
		$mybrands = array();
		if ($this->userinfo) {
			$mybrands = $this->Brand_model->getmybrands();
		}
		//－1 为删除状态		
		$this->db->where('status !=', '-1');
		$query = $this->db->get('brand');
		foreach ($query->result_array() as $row) {
			$row['logourl'] = base_url().'upload/logo/'.$row['logo'];
			if ($mybrands['brands'] && in_array($row['bid'], $mybrands['brands'])) {
				$row['isselected'] = 1;
			}else{
				$row['isselected'] = 0;				
			}
			// 分类
			$row['catname'] = $row['catid'] ? $this->category[$row['catid']]['name'] : '未分类';
			$data['brands'][$row['bid']] = $row;
		}
		$data['mybrands'] = $mybrands;
		return $data;
	}
	
	function orderbrand($data){
		if ($data['email'] && $data['brandids']) {
			$this->db->where('uid', $this->userinfo['uid']);
			$isexist = $this->db->count_all_results('orderbrand');
			if ($isexist) {
				$updatedata = array(
						'email' => $data['email'],
						'brands' => serialize($data['brandids']),
						'dateline' => now()
					);
				$this->db->update('orderbrand', $updatedata);
			}else{
				$insertdata = array(
						'uid' => $this->userinfo['uid'],
						'email' => $data['email'],
						'brands' => serialize($data['brandids']),
						'dateline' => now()
					);
				$this->db->insert('orderbrand', $insertdata);
			}
			return $this->db->affected_rows();
		}else{
			return false;
		}
	}
	
	function getmybrands(){
		$this->db->where('uid', $this->userinfo['uid']);
		$query = $this->db->get('orderbrand');
		$mybrands = $query->row_array();
		$mybrands['brands'] = unserialize($mybrands['brands']);
		return $mybrands;		
	}
	
	function insert_brand($data){
		if ($data) {
			$this->db->insert('brand', $data);
			return $this->db->insert_id();
		}
		return false;
	}
	
	function update_brand($data){
		if (!empty($data['bid'])) {
			$this->db->where('bid', $data['bid']);
			unset($data['bid']);
			$this->db->update('brand', $data);
			return $this->db->affected_rows();
		}
		return false;
	}
	
	function deletes_brand($ids){
		if($ids){
			$this->db->where_in('bid', $ids);
			$this->db->limit(COUNT($ids));
			$this->db->update('brand', array("status" => "-1"));
			if($this->db->affected_rows() <= 0){
				return false;
			}
			return true;
		}
		return false;
	}
}
/* End of file welcome.php */
/* Location: ./application/controllers/index.php */

?>